package com.bianmaba.spring.data.jpa.domain;


import org.springframework.data.domain.PageImpl;

import java.util.ArrayList;

public class PageableResult extends Result {
    private PageImpl page;

    public PageableResult(PageImpl data) {
        super(data);
        this.success = true;
        this.page = data;
    }

    public long getTotal() {
        if (page == null) {
            return 0;
        }
        return page.getTotalElements();
    }

    public long getTotalPages() {
        if (page == null) {
            return 0;
        }
        return page.getTotalPages();
    }

    @Override
    public Object getData() {
        if (page == null) {
            return new ArrayList<>(0);
        }
        return page.getContent();
    }


    @Override
    public void setData(Object data) {
        super.setData(data);
        if (data != null && data instanceof PageImpl) {
            this.page = (PageImpl) data;
        }
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public boolean isSuccess() {
        return success;
    }
}